//
//  OffLineView.m
//  MaskMachine
//
//  Created by yuanyuan on 15-3-3.
//  Copyright (c) 2015年 hanjiaxue. All rights reserved.
//

#import "OffLineView.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
@implementation OffLineView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createrOffLineUI:CGRectMake(self.serialNumber.left, 0, 1, self.frame.size.height)];
        [self createrOffLineUI:CGRectMake(self.serialNumber.left, 0, SCREEN_WIDTH-20, 1)];
        
        self.serialNumber = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0.15625*SCREEN_WIDTH, self.frame.size.height)];
        self.serialNumber.textColor = [self colorWithHexString:@"#9C9C9C"];
        self.serialNumber.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.serialNumber];
        
        
        
        [self createrOffLineUI:CGRectMake(self.serialNumber.right, 0, 1, self.frame.size.height)];
        
        self.theAgent = [[UILabel alloc] initWithFrame:CGRectMake(_serialNumber.right, 0, 0.375*SCREEN_WIDTH, self.frame.size.height)];
        self.theAgent.textColor = [self colorWithHexString:@"#9C9C9C"];
        self.theAgent.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_theAgent];
        
        [self createrOffLineUI:CGRectMake(self.theAgent.right, 0, 1, self.frame.size.height)];
        
        self.phoneNumber = [[UILabel alloc] initWithFrame:CGRectMake(self.theAgent.right, 0, 0.1875*SCREEN_WIDTH, self.frame.size.height)];
        self.phoneNumber.textColor = [self colorWithHexString:@"#9C9C9C"];
        self.phoneNumber.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.phoneNumber];
        
        [self createrOffLineUI:CGRectMake(self.phoneNumber.right, 0, 1, self.frame.size.height)];
        
        if (SCREEN_WIDTH == 320) {
            self.orderAmount = [[UILabel alloc] initWithFrame:CGRectMake(_phoneNumber.right, 0, 0.21875*SCREEN_WIDTH, self.frame.size.height)];
        }else if (SCREEN_WIDTH == 375){
            self.orderAmount = [[UILabel alloc] initWithFrame:CGRectMake(_phoneNumber.right, 0, 0.22575*SCREEN_WIDTH, self.frame.size.height)];
        }else{
            self.orderAmount = [[UILabel alloc] initWithFrame:CGRectMake(_phoneNumber.right, 0, 0.232*SCREEN_WIDTH, self.frame.size.height)];
        }
        
        self.orderAmount.textColor = [self colorWithHexString:@"#9C9C9C"];
        self.orderAmount.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_orderAmount];
        [self createrOffLineUI:CGRectMake(self.orderAmount.right, 0, 1, self.frame.size.height)];
        [self createrOffLineUI:CGRectMake(self.phoneNumber.right, 0, 1, self.frame.size.height)];
        
        if (SCREEN_WIDTH == 320) {
            self.state = [[UILabel alloc] initWithFrame:CGRectMake(self.orderAmount.right, 0, 0.1875*SCREEN_WIDTH, self.frame.size.height)];
        }else if (SCREEN_WIDTH == 375){
            self.state = [[UILabel alloc] initWithFrame:CGRectMake(self.orderAmount.right, 0, 0.1945*SCREEN_WIDTH, self.frame.size.height)];
        }else{
            self.state = [[UILabel alloc] initWithFrame:CGRectMake(self.orderAmount.right, 0, 0.201*SCREEN_WIDTH, self.frame.size.height)];
        }
        self.state.textColor = [self colorWithHexString:@"#9C9C9C"];
        self.state.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.state];
        
        [self createrOffLineUI:CGRectMake(self.state.right, 0, 1, self.frame.size.height)];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
        line.backgroundColor = [UIColor grayColor];
        line.alpha = 0.4;
        [self addSubview:line];
        
        
    }
    return self;
}

-(void)createrOffLineUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1];
    [self addSubview:lines];
}
//传入字符串返回RGB
- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
